using Sustainalytics.ESG.Entities;
using Sustainalytics.Synchronization.Validation.ValidationRules.Common;
using System;

namespace Sustainalytics.Synchronization.Validation.ValidationRules.Comprehensive
{
    public class RelativePositionChangeRule : ComprehensiveRule<RPCompanyESGComputationResult>
    {
        public override Severity Severity => Severity.Red;
        public override BusinessResearchProduct BusinessResearchProduct => BusinessResearchProduct.Comprehensive;
        public override string Code => "RR4";
        public override string InformationalMessage => "The Relative Position of a company moves by two levels (i.e. from Leader to Average Performer, Underperformer or Laggard OR from Laggard to Average Performer, Outperformer or Leader.)";

        public RelativePositionChangeRule()
        {
            For(x => x.OverallPerformance).When(Validator).WithKey(x => x.Id).WithMessage("The company {0} #{1}# has more than two levels assessment change");
        }

        private bool Validator(
            RPCompanyESGComputationResult validatedEntity,
            RPCompanyESGComputationResult referenceEntity)
        {
            var performanceNow = (RPPerformance)GetValue(validatedEntity);
            var performanceThen = (RPPerformance)GetValue(referenceEntity);

            return Math.Abs(performanceThen.Assessment.Id - performanceNow.Assessment.Id) - 2 < 0;
        }
    }
}
